﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.UI;


namespace Liberty.Controls.Rules
{

    [Serializable]
    public class ValidationRuleSet : RuleSet
    {
        #region Constants
        private const string CREATE_SCRIPT = "var validationRuleSet = $create({0},{1});";
        private const string ADD_RULE_SCRIPT = "validationRuleSet.AddRule( rule{0} );";
        #endregion

        #region Event Handlers
        #endregion

        #region Methods
        public string Render()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            StringBuilder builder = new StringBuilder();
            builder.Append( String.Format( CREATE_SCRIPT, this.GetType().ToString(), serializer.Serialize( this ) ) );
            for( int i = 0; i < this.PrimRules.Count; i++ )
            {
                builder.AppendLine( String.Format( ADD_RULE_SCRIPT, i ) );
            }
            return builder.ToString();
        }
        #endregion

        #region Properties
        public string PostbackControl
        {
            get
            {
                return i_PostbackControl;
            }
            set
            {
                i_PostbackControl = value;
            }
        }
        #endregion

        #region Private Methods
        #endregion

        #region Private Properties
        #endregion

        #region Construction and Finalization
        public ValidationRuleSet( List<AbstractRule> rules, string postbackControl )
            : base( rules )
        {
            this.PostbackControl = postbackControl;
        }
        #endregion

        #region Data Elements
        private string i_PostbackControl;
        #endregion
    }
}
